package intranet.controllers;
import org.springframework.web.servlet.ModelAndView;
import intranet.models.*;
import intranet.tools.*;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class EventController extends HttpServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	/* IsNumber Function */
	public static boolean IsNumber(String num) {
        try {
            Integer.parseInt(num);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
	
	@RequestMapping("/news_events.html")
	public ModelAndView NewsEvents(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws SQLException,ServletException, IOException{
		ModelAndView mv=new ModelAndView("index");
		if(session.getAttribute("user_id")!=null){	
			String page= request.getParameter("page");
			NewsEventsModel NewsEvent=new NewsEventsModel();
			ResultSet rs=NewsEvent.SelectNewsEvents(0);
			ArrayList NewsEvents=this.resultSetToArrayList(rs);
			int RowCount=NewsEvents.size();
			//Passing Page Included
			mv.addObject("includethispage", "notice_board/news_events.jsp");
			// Passing Data Record
			mv.addObject("data", NewsEvents);
			page=(page==null || IsNumber(page)==false)?"1":page;
			// Pagination Link Elements
			int PageSize=10;
			int PageCount=((RowCount%PageSize)>0)?Math.round(RowCount/PageSize)+1:Math.round(RowCount/PageSize);
			page=(Integer.parseInt(page)<=0 ||Integer.parseInt(page)>PageCount)?"1":page;
			int StartingRow=(Integer.parseInt(page)-1)*PageSize;
			int EndingRow=(Integer.parseInt(page)*PageSize)-1;
			mv.addObject("page", page);
			mv.addObject("PageCount", PageCount);
			mv.addObject("StartingRow", StartingRow);
			mv.addObject("EndingRow", EndingRow);
		}else{
			response.sendRedirect("login.html");
		}
		return mv;
	}
	
	@RequestMapping("/news_events_full_story.html")
	public ModelAndView NewsEventsFullStory(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws SQLException,ServletException, IOException{
		ModelAndView mv=new ModelAndView("index");
		if(session.getAttribute("user_id")!=null){	
		
			String id= request.getParameter("id");
			id=(id==null || IsNumber(id)==false)?"0":id;
			id=(Integer.parseInt(id)<=0)?"0":id;
			/* IF Requested ID=0 */
			if(Integer.parseInt(id)==0){
				return this.NewsEvents(request, response,session);
			}else{
				NewsEventsModel NewsEvent=new NewsEventsModel();
				ResultSet rs1=NewsEvent.SelectNewsEvents(Integer.parseInt(id));
				ResultSet rs2=NewsEvent.SelectNewsEventsImages(Integer.parseInt(id));
				ArrayList NewsEvents=this.resultSetToArrayList(rs1);
				ArrayList NewsEventsImages=this.resultSetToArrayList(rs2);
				//Passing Page Included
				mv.addObject("includethispage", "notice_board/news_events_full_story.jsp");
				// Passing Data Record & Image_File
				mv.addObject("data", NewsEvents);
				mv.addObject("images", NewsEventsImages);
				return mv;
			}
		}else{
			response.sendRedirect("login.html");
		}
		return mv;
	}
	// RESULTSET TO ARRAYLIST
	   public ArrayList resultSetToArrayList(ResultSet rs) throws SQLException{
		    ResultSetMetaData md = rs.getMetaData();
		    int columns = md.getColumnCount();
		    ArrayList results = new ArrayList();
		    while (rs.next()){
		    HashMap row = new HashMap();
		    results.add(row);
		    for(int i=1; i<=columns; i++){
		    row.put(md.getColumnName(i),rs.getObject(i));
		    }
		    }
		    return results;

	    }
}
